package com.ct.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@RequestMapping("/request")
@RestController
public class RequestController {

    @RequestMapping("/login")
    public String login (String name) {
        return name;
    }
    @RequestMapping(value = "/r2", method = RequestMethod.GET)
    public String r2 (String name) {
        return name;
    }

    @RequestMapping("/r3")
    public String r3 (String name, String password) {
        return "接收到参数 name : " + name + ", password : " + password;
    }

    @RequestMapping("/r4")
    public String r4 (Person person) {
        return "接收到参数 person : " + person;
    }

    @RequestMapping("/r5")
    public String r5 (@RequestParam("q") String keyword) {
        return "接收到参数 : " + keyword;
    }

    @RequestMapping("/r6")
    public String r6 (String[] arr) {
        return "接收到参数 : " + Arrays.toString(arr);
    }

    @RequestMapping("/r7")
    public String r7 (@RequestParam List<String> list) {
        return "接收到集合 : " + Arrays.toString(list.toArray());
    }

    @RequestMapping("/r8")
    public String r8 (@RequestBody Person person) {
        return "接收到JSON数据   : " + person;
    }

    @RequestMapping("/r9/atricle/{atricleId}")
    public String r9 (@PathVariable String atricleId) {
        return "接收到atricleId : " + atricleId;
    }

    @RequestMapping("/r10/atricle/{atricleId}/{type}")
    public String r10 (@PathVariable String atricleId, @PathVariable String type) {
        return "接收到atricleId : " + atricleId + ", 文章类型: " + type;
    }

    @RequestMapping("/r11/atricle/{Id}")
    public String r11 (@PathVariable("Id") String Id) {
        return "接收到ID : " + Id;
    }

    @RequestMapping("/r12")
    public String r12 (MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File distFile = new File("E:\\temp\\" + originalFilename);
        file.transferTo(distFile);
        return "存储文件成功.";
    }

    @RequestMapping("/r13")
    public String r13 (@RequestPart("file111") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File distFile = new File("E:\\temp\\" + originalFilename);
        file.transferTo(distFile);
        return "存储文件成功.";
    }

    @RequestMapping("/getCookie")
    public String getCookie (HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) System.out.println(cookie.getName() + " : " + cookie.getValue());
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2 (@CookieValue("name") String name) {
        return "获取cookie成功 : " + name;
    }

    @RequestMapping("/setSession")
    public String setSession (HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name", "java114");
        session.setAttribute("age", 1);
        return "设置Session成功";
    }

    @RequestMapping("/getSession")
    public String getSession (HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "获取session成功: name = " + name + ", age = " + age;
    }

    @RequestMapping("/getSession2")
    public String getSession2 (HttpSession session) {
        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "获取session成功: name = " + name + ", age = " + age;
    }

    @RequestMapping("/getSession3")
    public String getSession3 (@SessionAttribute("name") String name) {
        return "获取session成功: name = " + name;
    }

    @RequestMapping("/getHeader")
    public String getHeader (HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "获取header成功: userAgent = " + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2 (@RequestHeader("User-Agent") String userAgent ) {
        return "获取header成功: userAgent = " + userAgent;
    }
}
